<?php
namespace Home\Controller;
use Think\Controller;
class PublicController extends CommonController{
	public function index(){
		$this->display();
	}
	
	public function nav(){
		$action = session("action");
		$menu_id = session("menu_id");
		
		$where = '';
    	foreach (session("menu") as $value){
    		$where .= "menu_id = ".$value." or ";
    	}
    	$where = substr($where, 0, -4);
    	$sql = "SELECT menu_id, menu_name, action FROM oa_sys_menu WHERE leve = $menu_id and status = 1 and pid = 0 and (".$where.") order by sort";
    	$data = M()->query($sql);
    	$list = array();
    	foreach ($data as $key=>$value){
    		$menu_id1 = $value['menu_id'];
    		$sql = "SELECT menu_id, menu_name,action FROM oa_sys_menu WHERE leve = $menu_id and status = 1 and pid = $menu_id1 and (".$where.") order by sort";
    		$list[$key] = M()->query($sql);
    	}
    	$this->assign("data", $data);
		$this->assign("list", $list);
		
		$this->display();
	}
	
	public function lists(){
		$model = M();
		$menu_id = session("menu_id");
		$where = '';
    	foreach (session("menu") as $value){
    		$where .= "menu_id = ".$value." or ";
    	}
    	$where = substr($where, 0, -4);
    	$sql = "SELECT menu_id, menu_name,action FROM oa_sys_menu WHERE leve = $menu_id and status = 1 and pid = 0 and (".$where.") order by sort";
    	$data = $model->query($sql);
    	$menu_id_default = $data[0]['menu_id'];
    	if(I('get.id') == "undefined"){
    		$id = $menu_id_default;
    	}else{
    		$id = I('get.id');
    	}
		$sql = "SELECT menu_id, menu_name,action FROM oa_sys_menu WHERE leve = $menu_id and status = 1 and pid = $id order by sort";
		$data1 = M()->query($sql);
		if(empty($data1)){
			$sql = "SELECT menu_id, menu_name,action FROM oa_sys_menu WHERE leve = $menu_id and status = 1 and menu_id = $id order by sort";
			$data2 = M()->query($sql);
			$action = $data2[0]['action'];
		}else{
			$action = $data1[0]['action'];
		}
//		echo $sql;
//		echo $action;exit;
		$this->redirect($action);
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
	
}
?>